<template>
  <div class="container">
    <el-upload
      v-show="!isShowProgress"
      :before-upload="beforeUpload"
      :class="{display:fileList.length}"
      :file-list="fileList"
      :http-request="onRequest"
      :limit=1
      :on-change="onChange"
      :on-preview="handlePictureCardPreview"
      :on-remove="onRemove"
      accept="image/png,image/jpg,image/jpeg,image/gif"
      action="#"
      class="upload"
      list-type="picture-card">
      <!--      <el-button v-if="!fileList.length" class="upLoadBtn">上传图片</el-button>-->
      <span @click="click">点击上传</span>
      <span v-if="!fileList.length" class="closeUpload el-icon-circle-close"></span>
    </el-upload>

    <div v-if="isShowProgress" style="margin-left: 30px; display: flex; align-items: center;">
      <el-progress :percentage="percentage" type="circle"/>
    </div>
    <el-dialog :visible.sync="dialogVisible">
      <img :src="dialogImageUrl" alt="" width="100%">
    </el-dialog>
  </div>
</template>

<script>
import COS from 'cos-js-sdk-v5'

const cos = new COS({
  SecretId: 'AKIDMXRAcojNlsjJdhU1HGyPZS4PTGmpb8ON',
  SecretKey: 'jBNIvIODl22mGTP4RXMvCCvOaV8UaPxE'
})
export default {
  name: 'UploadImage',
  data () {
    return {
      fileList: [],
      currentUId: '',
      percentage: 0,
      isShowProgress: false,
      dialogImageUrl: '',
      dialogVisible: false
    }
  },
  methods: {
    click () {
      console.log(this.fileList)
    },
    // 点击删除
    onRemove (file, fileList) {
      // this.fileList.filter(item => item.uid !== file.uid)
      this.fileList = fileList
    },
    handlePictureCardPreview (file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    // 自定义上传请求
    onRequest (data) {
      cos.putObject({
        Bucket: 'hmmm-1307760716', /* 必须 */
        Region: 'ap-shanghai', /* 存储桶所在地域，必须字段 */
        Key: data.file.name, /* 必须 */
        StorageClass: 'STANDARD',
        Body: data.file, // 上传文件对象
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))
          this.percentage = progressData.percent * 100
        }
      }, (err, data) => {
        console.log(err || data)
        if (err) {
          this.$message.error('上传失败,请重新上传')
          this.fileList = this.fileList.filter(item => item.uid !== this.currentUId)
          this.isShowProgress = false
        }
        if (!err && data.statusCode === 200) {
          const url = 'https://' + data.Location
          this.fileList.forEach(item => {
            if (item.uid === this.currentUId) {
              item.url = url
            }
            return item
          })
          // this.$emit('on-success', this.fileList, url)
          this.$emit('on-success', url)
          // console.log(this.fileList)
          this.$message.success('上传图片成功')
          setTimeout(() => {
            this.isShowProgress = false
          }, 1000)
        }
      })
    },
    // 本地上传成功
    onChange (file, fileList) {
      this.fileList = fileList
    },
    // 选择图片上传之前
    beforeUpload (file) {
      const type = ['image/gif', 'image/png', 'image/jpg', 'image/jpeg']
      if (!type.includes(file.type)) {
        this.$message.error('请选择正确格式图片')
        return false
      }
      const maxSize = 500 * 1024
      if (file.size >= maxSize) {
        this.$message.error('请选择500kb以内的图片')
        return false
      }
      this.currentUId = file.uid
      this.isShowProgress = true
      return true
    }
  }
}
</script>

<style lang="less" scoped>
.container {
  display: inline-block;

  /deep/ .el-progress-circle {
    width: 60px !important;
    height: 60px !important;
  }

  /deep/ .display .el-upload--picture-card {
    display: none !important;

  }

  /deep/ .el-upload-list__item {
    width: 100px !important;
    vertical-align: middle !important;
    background-color: #fff !important;
    border: none !important;
    line-height: 60px;
    height: 60px !important;
  }

  /deep/ .el-upload--picture-card {
    width: 100px !important;
    vertical-align: middle !important;
    background-color: #fff !important;
    line-height: 60px;
    height: 60px !important;
    display: inline-block;
    position: relative;
    border: 1px dashed #d9d9d9;
  }

  /deep/ .el-upload-input {
    width: 100px;
    height: 60px;
    z-index: 100;
  }

  ///deep/ .upLoadBtn {
  //  display: inline-block;
  //  position: relative;
  //  width: 100px;
  //  height: 60px;
  //  border: 1px dashed #d9d9d9;
  //}

  /deep/ .upload {
    display: inline-block;
    position: relative;
    margin-left: 5px;
  }

  /deep/ .closeUpload {
    position: absolute;
    right: -1px;
    top: -7px;
    display: block;
    width: 10px;
    height: 10px;
    font-size: 16px;
    border-radius: 50%;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    cursor: pointer;
    color: #999;
  }

  /deep/ .el-upload--picture-card:hover {
    border: 1px dashed #6b8dfc;
  }

  /deep/ .el-upload-list__item-status-label {
    display: none;
  }
}
</style>
